import axios from "axios";
import { getToken } from "xqjr-module-auth";
export default function(params) {
  // const {} = params;
  return {
    data() {
      return {
        id: "",
        fileList: [],
        data: {},
        uploading: false,
        headers: {},
        Faildata: [],
        action: "",
        dialogFailVisible: false,
        numAll: 0,
        numFail: 0,
        numSuc: 0
      };
    },
    created() {
      this.headers = {
        token: getToken(),
        "Content-Type": "multipart/form-data"
      };
      this.id = this.$store.state.hr.companyId;
    },
    methods: {
      handleRemove(file) {
        const index = this.fileList.indexOf(file);
        const newFileList = this.fileList.slice();
        newFileList.splice(index, 1);
        this.fileList = newFileList;
      },
      beforeUpload(file) {
        if (this.fileList.length > 0) {
          this.$message("最多只能上传一个");
          return false;
        }
        this.fileList = [...this.fileList, file];
        if (this.fileList && this.fileList.length > 0) {
          const { fileList } = this;
          const formData = new FormData();
          fileList.forEach(file => {
            formData.append("file", file);
          });
          this.uploading = true;

          axios({
            url: this.action,
            method: "post",
            data: formData,
            headers: this.headers
          }).then(res => {
            this.reset();
            this.handleSuccess(res.data);
            this.uploading = false;
          });
        } else {
          this.reset();
          this.handleUploadError();
        }
        return false;
      },
      handleFailClose() {
        this.dialogFailVisible = false;
      },
      handleSuccess(res) {
        var that = this;
        if (res.success && res.data !== null) {
          that.numAll = res.data.importFailList.length + res.data.importSucList.length;
          that.numFail = res.data.importFailList.length;
          that.numSuc = res.data.importSucList.length;
          that.Faildata = res.data.importFailList;
          that.showFaild();

          // this.$confirm({
          //   okText: "确定",
          //   cancelText: "查看未成功名单",
          //   title: `共导入${numAll}人，${numSuc}人成功，${numFail}人未成功`,
          //   onOk() {
          //     that.goback();
          //   },
          //   onCancel() {
          //     that.Faildata = res.data.importFailList;
          //     that.showFaild();
          //     // that.dialogFailVisible = true;
          //   }
          // });
        } else if (res.data === null) {
          this.$confirm({
            okText: "确定",
            title: res.msg
          });
        } else {
          this.$notification["error"]({
            message: "error",
            description: res.msg || "上传失败"
          });
          setTimeout(() => {
            this.reset();
          }, 2000);
        }
      },
      goback() {
        this.$router.go(-1);
      },
      reset() {
        this.fileList = [];
      }
    }
  };
}
